package com.hnevc.androidui7;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity01 extends AppCompatActivity {

    Button mBtn1,mBtn2; //保存获取到的按钮对象
    TextView mTvMessage; //保存获取到的文本标签对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_01);

        mTvMessage = findViewById(R.id.tv_message);
        mBtn2 = findViewById(R.id.btn_button2);
        mBtn1 = findViewById(R.id.btn_button1);//从布局文件中找到id为R.id.btn_button1的控件
        mBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建一个intent，使用startActivityForResult()启动Activity02
                Intent intent = new Intent(Activity01.this, Activity02.class);
                startActivityForResult(intent, 1001);
            }
        });
        mBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建一个intent，使用startActivityForResult()启动Activity02
                Intent intent = new Intent(Activity01.this, Activity03.class);
                startActivityForResult(intent, 1010);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data!=null){
            if(requestCode == 1001){
                //由Activity02返回的数据
                if(resultCode == 1002){
                    String message  = data.getStringExtra("data");//获取回传的intent数据
                    //显示回传的信息到文本框中
                    mTvMessage.setText(message);
                }else if(resultCode == 1003){

                }
            }
            else if(requestCode == 1010){
                //由Activity03返回的数据
            }
        }
    }
}
